package ink.xiaobaibai.controller.onePay;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import ink.xiaobaibai.common.MySecurity;
import ink.xiaobaibai.entity.AdminUser;
import ink.xiaobaibai.entity.ClientOrder;
import ink.xiaobaibai.entity.OzonePrice;
import ink.xiaobaibai.response.ResponseFormat;
import ink.xiaobaibai.service.IAdminUserService;
import ink.xiaobaibai.service.IClientOrderService;
import ink.xiaobaibai.service.IOzonePriceService;
import ink.xiaobaibai.vo.ClientOrderItemVo;
import ink.xiaobaibai.vo.ClientOrderVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @description: 订单接口
 * @author: 小白白
 * @create: 2021-05-20
 **/

@Api(tags = "订单接口")
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private IClientOrderService clientOrderService;

    @Autowired
    private IAdminUserService adminUserService;

    @Autowired
    private IOzonePriceService priceService;

    @GetMapping("/list")
    @ApiOperation("获取自己的单次订单列表(分页)")
    public ResponseFormat getOrderList(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                       @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        List<ClientOrderVo> list = this.clientOrderService.getClientListByUserId(MySecurity.getUserId(), pageNum, pageSize);
        return ResponseFormat.success(list);
    }

    /**
     * 订单详细
     * 订单id 订单金额 机器id 创建时间 负责人姓名 负责人电话
     */
    @GetMapping("/item")
    @ApiOperation("订单详细")
    public ResponseFormat getOrderItem(@RequestParam("orderId") String orderId) {
        QueryWrapper<ClientOrder> q1 = new QueryWrapper<>();
        q1.select("order_amount", "device_id", "admin_user_id", "create_time");
        q1.eq("order_id", orderId);
        ClientOrder clientOrder = this.clientOrderService.getOne(q1);
        QueryWrapper<AdminUser> q2 = new QueryWrapper<>();
        q2.select("user_name", "user_phone");
        q2.eq("id", clientOrder.getAdminUserId());
        AdminUser adminUser = this.adminUserService.getOne(q2);
        QueryWrapper<OzonePrice> q3 = new QueryWrapper<>();
        q3.eq("device_id", clientOrder.getDeviceId());
        return ResponseFormat.success(new ClientOrderItemVo(orderId, clientOrder.getOrderAmount(), clientOrder.getDeviceId(), clientOrder.getCreateTime(), adminUser.getUserName(), adminUser.getUserPhone(), clientOrder.getRunTime()));
    }

}
